<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="/plugins/select2/select2.css" />
	<link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>
                    <div class="box-body">
                  			 <ol class="breadcrumb">	                        	
                        		<li>
		                        	<a href="javascript:void(0)" onclick="listItemCatOneLevel()">顶级分类列表</a>
		                        </li>
		                        <li>
		                       		<a href="javascript:void(0)" onclick="listItemCatTwoLevel()">珠宝</a>
		                        </li>
		                        <li>
		                        	<a href="javascript:void(0)" onclick="listItemCatThreeLevel()">银饰</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="delItems()" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemCatBody">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		   珠宝 >>  银饰
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input id="itemName" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td id="typeTemplateBody">
<!--		      			<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="saveItemCat()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  </div>

</body>
</html>
<script>
	$(function () {
		//分类集合
		listItemCatOneLevel()
		//类型模板集合
		listTypeTemplates()
		listTypeTemplates2()
	})
	function listTypeTemplates() {
		$.ajax({
			url:"/typeTemplateController/listTypeTemplates",
			data: {},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var templates = result.data;
					var html='<select id="typeSelect"  style="width: 100%">';
					$(templates).each(function (i,e) {
						html+='<option value="'+e.id+'">'+e.name+'</option>'
					})
					html+='</select>'
					$("#typeTemplateBody").html(html)
					$("#typeSelect").select2()
				}else {
					alert(result.message)
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
	}
	function listTypeTemplates2() {
		$.ajax({
			url:"/typeTemplateController/listTypeTemplates",
			data: {},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var templates = result.data;
					var html='<select id="typeSelect2"  style="width: 100%">';
					$(templates).each(function (i,e) {
						html+='<option value="'+e.id+'">'+e.name+'</option>'
					})
					html+='</select>'
					$("#typeTemplateBody2").html(html)
					$("#typeSelect2").select2()
				}else {
					alert(result.message)
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
	}
	function saveItemCat() {
		$.ajax({
			url:"/itemCatController/saveItemCat",
			data:{typeId:$("#typeSelect").val(),name:$("#itemName").val(),parentId:$("#parentId").val()},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listItemCatOneLevel(id) {
		if (id == null){
			id = 0;
		}
		$.ajax({
			url:"/itemCatController/listItemCat",
			data: {parentId:id},
			dataType:"json",
			type:"post",
			success:function (result) {
				var html='<input type="hidden" id="parentId" value="'+id+'">'
				if (result.code == 10000){
					var listTtem = result.data
					$(listTtem).each(function (i,e) {
						html+='<tr >'
						html+='<td><input name="box" value="'+e.id+'" type="checkbox" ></td>'
						html+='<td>'+e.id+'</td>'
						html+='<td>'+e.name+'</td>'
						html+='<td>'+e.typeId+'</td>'
						html+='<td class="text-center">'
						html+='<button onclick="listItemCatOneLevel('+e.id+')" type="button" class="btn bg-olive btn-xs" >查询下级</button>'
						html+='<button type="button" onclick="toUpdate('+e.id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>'
						html+='</td>'
						html+='</tr>'
					})
					$("#itemCatBody").html(html)
				}else {
					html+='<h1>'+result.message+'</h1>'
					$("#itemCatBody").html(html)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function toUpdate(id) {
		$.ajax({
			url:"/itemCatController/selectItemById",
			data:{id:id},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				var html =''
				var itemList = result.data
				html+='<div class="modal-dialog" >'
				html+='<div class="modal-content">'
				html+='<div class="modal-header">'
				html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
				html+='<h3 id="myModalLabel">商品分类编辑</h3>'
				html+='</div>'
				html+='<div class="modal-body">'
				html+='<table class="table table-bordered table-striped"  width="800px">'
				html+='<tr>'
				html+='<td>上级商品分类</td>'
				html+='<td>珠宝 >>  银饰</td>'
				html+='</tr>'
				html+='<tr>'
				html+='<td>商品分类名称</td>'
				html+='<td><input id="itemName2" value="'+itemList.name+'" class="form-control" placeholder="商品分类名称">  </td>'
				html+='</tr>'
				html+='<tr>'
				html+='<td>类型模板</td>'
				html+='<td id="typeTemplateBody2">'
				html+='</td>'
				html+='</tr>'
				html+='</table>'
				html+='</div>'
				html+='<div class="modal-footer">'
				html+='<button class="btn btn-success" onclick="updateItemCat('+itemList.id+')" data-dismiss="modal" aria-hidden="true">保存</button>'
				html+='<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>'
				html+='</div>'
				html+='</div>'
				html+='</div>'

				$("#updateModal").html(html)
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function updateItemCat(id) {
		$.ajax({
			url:"/itemCatController/updateItemCat",
			data:{id:id,typeId:$("#typeSelect2").val(),name:$("#itemName2").val()},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function delItems() {
		if (confirm("确定要删除本节点及以下的所有节点吗?")){
			var boxs = document.getElementsByName("box")
			var ids = ''
			$(boxs).each(function (i,e) {
				if (e.checked){
					ids += ","+$(e).val()
				}
			})
			ids = ids.substring(1,ids.length)
			$.ajax({
				url:"/itemCatController/delItems",
				data:{ids:ids},
				dataType:"json",
				type:"post",
				async:false,
				success:function (result) {
					if (result.code == 10000){
						alert(result.message)
						window.location.reload();
					}else {
						alert(result.message)
					}
				},
				error:function (result) {
					alert("系统错误")
				}
			})
		}
	}
</script>